﻿Public Class frmFNR
    Public Enum FileScope
        CurrentFile = 0
        AllFiles = 1
    End Enum

    Public Enum SearchScope
        FromCursor = 0
        EntireScope = 1
    End Enum

    Public SearchFiles As New ArrayList

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Try
            Dim CurrentFileScope As FileScope = FileScope.CurrentFile
            Dim CurrentSearchScope As SearchScope = SearchScope.FromCursor

            If (rbAllFiles.Checked = True) Then CurrentFileScope = FileScope.AllFiles
            If (rbEntireScope.Checked = True) Then CurrentSearchScope = SearchScope.EntireScope

            PopulateSearchFiles(CurrentFileScope)

            FindAndReplace(CurrentSearchScope, False)

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Public Sub PopulateSearchFiles(ByVal Scope As FileScope)
        Try
            SearchFiles.Clear()

            If (frmMain.tcMain.TabPages.Count = 0) Then Exit Sub

            Select Case Scope
                Case FileScope.AllFiles
                    For Each t As TabPage In frmMain.tcMain.TabPages
                        SearchFiles.Add(t.Controls("txtEditArea"))
                    Next

                Case FileScope.CurrentFile
                    SearchFiles.Add(frmMain.tcMain.SelectedTab.Controls("txtEditArea"))

            End Select

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Public Sub FindAndReplace(ByVal Scope As SearchScope, ByVal bReplace As Boolean, Optional ByVal bReplaceAll As Boolean = False)
        Try
            frmMain.ProgramState = frmMain.State.FindReplace
            Dim iNumFound As Integer = 0
            Dim iNumFiles As Integer = 0

            For Each r As RichTextBox In SearchFiles
                If (bReplaceAll = False) Then
                    Dim iSearchStart As Integer = 0

                    If (Scope = SearchScope.FromCursor) Then
                        Dim iCursorPos As Integer = r.SelectionStart

                        If (iCursorPos < r.Text.Length) Then iSearchStart = iCursorPos
                    End If

                    If (iSearchStart < 0) Then iSearchStart = 0

                    Dim iWordPos As Integer = r.Text.IndexOf(txtFind.Text, iSearchStart)
                    Dim iWordLength As Integer = txtFind.Text.Length

                    If (iWordPos <> -1) Then
                        If (bReplace = True) Then
                            Dim iReplaceWordPos As Integer = r.Rtf.IndexOf(txtFind.Text, iSearchStart)

                            If (iReplaceWordPos <> -1) Then
                                r.Rtf = r.Rtf.Insert(iReplaceWordPos, txtReplace.Text)
                                r.Rtf = r.Rtf.Remove(iReplaceWordPos + txtReplace.Text.Length, iWordLength)
                            End If

                            iWordLength = txtReplace.Text.Length
                        End If

                        r.SelectionStart = iWordPos
                        r.SelectionLength = iWordLength
                        r.ScrollToCaret()

                        iNumFound += 1
                    End If

                ElseIf (bReplaceAll = True) Then
                    Dim iNumWords = CountWordInString(txtFind.Text, r.Rtf)
                    iNumFound += iNumWords

                    r.Rtf = r.Rtf.Replace(txtFind.Text, txtReplace.Text)

                    If (iNumWords > 0) Then iNumFiles += 1
                End If
            Next

            If (iNumFound = 0) Then
                MsgBox("No occurences found...")

            ElseIf (bReplaceAll = True) Then
                MsgBox(iNumFound.ToString() + " occurences replaced in " + iNumFiles.ToString() + " file(s).")

                For Each p As DictionaryEntry In frmMain.OpenProjects
                    p.Value.UpdateEvents(False)
                Next

                frmMain.UpdateEventList(frmMain.tcMain.SelectedTab.Text)
                frmMain.UpdateTodoList(frmMain.tcMain.SelectedTab.Text)
            End If

            frmMain.ProgramState = frmMain.State.Idle

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Public Function CountWordInString(ByVal strWord As String, ByVal strString As String) As Integer
        Try
            Dim iWordPos As Integer = strString.IndexOf(strWord)
            Dim iCount As Integer = 0

            If (iWordPos = -1) Then Return 0

            While (iWordPos <> -1)
                iCount += 1

                If (iWordPos + 1 >= strString.Length) Then Exit While

                iWordPos = strString.IndexOf(strWord, iWordPos + 1)
            End While

            Return iCount

        Catch ex As Exception
            frmMain.HandleException(ex)

            Return 0
        End Try
    End Function

    Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
        Try
            Dim CurrentFileScope As FileScope = FileScope.CurrentFile
            Dim CurrentSearchScope As SearchScope = SearchScope.FromCursor

            If (rbAllFiles.Checked = True) Then CurrentFileScope = FileScope.AllFiles
            If (rbEntireScope.Checked = True) Then CurrentSearchScope = SearchScope.EntireScope

            PopulateSearchFiles(CurrentFileScope)

            FindAndReplace(CurrentSearchScope, True)

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceAll.Click
        Try
            Dim CurrentFileScope As FileScope = FileScope.CurrentFile
            Dim CurrentSearchScope As SearchScope = SearchScope.FromCursor

            If (rbAllFiles.Checked = True) Then CurrentFileScope = FileScope.AllFiles
            If (rbEntireScope.Checked = True) Then CurrentSearchScope = SearchScope.EntireScope

            PopulateSearchFiles(CurrentFileScope)

            FindAndReplace(CurrentSearchScope, True, True)

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub
End Class